/*
 * Created on 29/06/2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JToolBar;
import javax.swing.text.Position;

import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.GridModelEqController;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.ModelEqController;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdMenu;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.DrawingArea;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.IView;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnView;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnViewState;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.SelectCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command.GetModelForcesCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command.GetModelNodalForcesCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command.GetModelReduceForcesCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command.GetModelReduceNodalForcesCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command.GetModelReduceStiffnessCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command.GetModelStiffnessCommand;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command.Help5Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command.ModelEquilibriumCommand;

/**
 * @author Francisca
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ModelView extends LearnView {  
	
	//  No da arvore: Model Equilibrium
	private Command modelStiffnessCmd = new GetModelStiffnessCommand(parent);
	private Command modelForcesCmd = new GetModelForcesCommand(parent);
	private Command modelNodalForcesCmd = new GetModelNodalForcesCommand(parent);
	private Command modelReduceStiffnessCmd = new GetModelReduceStiffnessCommand(parent);
	private Command modelReduceForcesCmd = new GetModelReduceForcesCommand(parent);
	private Command modelNodalReduceForcesCmd = new GetModelReduceNodalForcesCommand(parent);
	private Command modelEquilibriumCmd = new ModelEquilibriumCommand(parent);
	private Command help5Cmd = new Help5Command(parent);
	
	//	No Model Equilibrium
    private CmdMenu itemModelStiffness = new CmdMenu("Model Stiffness", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/StifinessM1.gif")));
    private CmdMenu itemModelForces = new CmdMenu("Model Forces", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/ForcM1.gif")));
    private CmdMenu itemModelNodalForces = new CmdMenu("Model Nodal Forces", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/NodalforcM1.gif")));
    private CmdMenu itemModelReduceStiffness = new CmdMenu("Model Reduce Stiffness", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/StifinessRedM1.gif")));
    private CmdMenu itemModelReduceForces = new CmdMenu("Model Reduce Forces", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/ForcRedM1.gif")));
    private CmdMenu itemModelNodalReduceForces = new CmdMenu("Model Reduce Nodal Forces", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/NodalforcredM1.gif")));
    private CmdMenu itemModelEquilibrium = new CmdMenu("Model Equilibrium Conditions", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/EquilM1.gif")));
    private CmdMenu itemHelp5 = new CmdMenu("Help", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Help16.gif")));

    
    //No Model Equilibrium
    private CmdButton modelStiffness = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/StifinessM1.gif")));
    private CmdButton modelForces = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/ForcM1.gif")));
    private CmdButton modelNodalForces = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/NodalforcM1.gif")));
    private CmdButton modelReduceStiffness = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/StifinessRedM1.gif")));
    private CmdButton modelReduceForces = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/ForcRedM1.gif")));
    private CmdButton modelReduceNodalForces = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/NodalforcredM1.gif")));
    private CmdButton modelEquilibrium = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/EquilM1.gif")));
    private CmdButton help5 = new CmdButton(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Help16.gif")));
   
    private JMenu menuModelEquilibrium = new JMenu("Model Equilibrium");
    protected JToolBar modelEquilibriumToolBar = new JToolBar();
    
	public ModelView(LearnInterface parent, FemModel m){
	    super(parent,m);
		super.setViewState(new LearnViewState());
		super.setDrawingArea(new DrawingArea((IView)this));
		super.setController(new ModelEqController(this));
		
		String gAnlType = m.getGlobalAnalysisModel().getType();
		
		if (!(gAnlType.equals("Grid"))) {
			super.setController(new ModelEqController(this));
		} else {
			super.setController(new GridModelEqController(this));
		}
		
		//Setting menu Model Equilibrium
		menuModelEquilibrium.add(itemModelStiffness);
		itemModelStiffness.setCommand(modelStiffnessCmd);
		itemModelStiffness.setEnabled(false);
		itemModelStiffness.setMnemonic(KeyEvent.VK_S);
		itemModelStiffness.addActionListener(parent);
        
		menuModelEquilibrium.add(itemModelForces);
		itemModelForces.setCommand(modelForcesCmd);
		itemModelForces.setEnabled(false);
		itemModelForces.setMnemonic(KeyEvent.VK_F);
		itemModelForces.addActionListener(parent);
		
		menuModelEquilibrium.add(itemModelNodalForces);
		itemModelNodalForces.setCommand(modelNodalForcesCmd);
		itemModelNodalForces.setEnabled(false);
		itemModelNodalForces.setMnemonic(KeyEvent.VK_F);
		itemModelNodalForces.addActionListener(parent);
		
		menuModelEquilibrium.add(itemModelReduceStiffness);
		itemModelReduceStiffness.setCommand(modelReduceStiffnessCmd);
		itemModelReduceStiffness.setEnabled(false);
		itemModelReduceStiffness.setMnemonic(KeyEvent.VK_S);
		itemModelReduceStiffness.addActionListener(parent);
        
		menuModelEquilibrium.add(itemModelReduceForces);
		itemModelReduceForces.setCommand(modelReduceForcesCmd);
		itemModelReduceForces.setEnabled(false);
		itemModelReduceForces.setMnemonic(KeyEvent.VK_F);
		itemModelReduceForces.addActionListener(parent);
		
		menuModelEquilibrium.add(itemModelNodalReduceForces);
		itemModelNodalReduceForces.setCommand(modelNodalReduceForcesCmd);
		itemModelNodalReduceForces.setEnabled(false);
		itemModelNodalReduceForces.setMnemonic(KeyEvent.VK_F);
		itemModelNodalReduceForces.addActionListener(parent);
		
		menuModelEquilibrium.add(itemModelEquilibrium);
		itemModelEquilibrium.setCommand(modelEquilibriumCmd);
		itemModelEquilibrium.setEnabled(false);
		itemModelEquilibrium.setMnemonic(KeyEvent.VK_B);
		itemModelEquilibrium.addActionListener(parent);
		
		menuModelEquilibrium.add(itemHelp5);
        itemHelp5.setCommand(help5Cmd);
        itemHelp5.setEnabled(false);
        itemHelp5.setMnemonic(KeyEvent.VK_H);
        itemHelp5.addActionListener(parent);
        
        modelEquilibriumToolBar.add(modelStiffness);
        modelStiffness.setToolTipText("Model Stiffness");
        modelStiffness.addActionListener(parent);
        modelStiffness.setCommand(modelStiffnessCmd);
        
        modelEquilibriumToolBar.add(modelForces);
        modelForces.setToolTipText("Model Forces");
        modelForces.addActionListener(parent);
        modelForces.setCommand(modelForcesCmd);
        
        modelEquilibriumToolBar.add(modelNodalForces);
        modelNodalForces.setToolTipText("Model Nodal Forces");
        modelNodalForces.addActionListener(parent);
        modelNodalForces.setCommand(modelNodalForcesCmd);

        modelEquilibriumToolBar.add(modelReduceStiffness);
        modelReduceStiffness.setToolTipText("Model Reduce Stiffness");
        modelReduceStiffness.addActionListener(parent);
        modelReduceStiffness.setCommand(modelReduceStiffnessCmd);
        
        modelEquilibriumToolBar.add(modelReduceForces);
        modelReduceForces.setToolTipText("Model Reduce Forces");
        modelReduceForces.addActionListener(parent);
        modelReduceForces.setCommand(modelReduceForcesCmd);
        
        modelEquilibriumToolBar.add(modelReduceNodalForces);
        modelReduceNodalForces.setToolTipText("Model Reduce Nodal Forces");
        modelReduceNodalForces.addActionListener(parent);
        modelReduceNodalForces.setCommand(modelNodalReduceForcesCmd);
        
        modelEquilibriumToolBar.add(modelEquilibrium);
        modelEquilibrium.setToolTipText("Model Equilibrium");
        modelEquilibrium.addActionListener(parent);
        modelEquilibrium.setCommand(modelEquilibriumCmd);
               
        modelEquilibriumToolBar.add(help5);
		help5.setToolTipText("Help");
		help5.addActionListener(parent);
		help5.setCommand(help5Cmd);
        
	}
	
	public void init(){
	    super.init();
			parent.getMenuPanel().removeAll();
			parent.getMenuPanel().add(menuFile);
			itemSave.setEnabled(true);
			itemSaveAs.setEnabled(true);
			itemClose.setEnabled(true);
			itemPrint.setEnabled(true);
			menuExport.setEnabled(true);
			menuImport.setEnabled(true);
			parent.getMenuPanel().add(menuEdit);
			parent.getMenuPanel().add(menuView);
			parent.getMenuPanel().add(menuModelEquilibrium);
			menuModelEquilibrium.setEnabled(true);
			itemModelStiffness.setEnabled(true);
			itemModelForces.setEnabled(true);
			itemModelNodalForces.setEnabled(true);
			itemModelReduceStiffness.setEnabled(true);
			itemModelReduceForces.setEnabled(true);
			itemModelNodalReduceForces.setEnabled(true);
			itemModelEquilibrium.setEnabled(true);
			itemHelp5.setEnabled(true);
			parent.getMenuPanel().add(menuSettings);
			parent.getMenuPanel().add(menuHelp);
	        
			parent.getToolPanel().removeAll();
			parent.getToolPanel().add(fileToolBar);
			save.setEnabled(true);
			saveAs.setEnabled(true);
			print.setEnabled(true);
			delete.setEnabled(false);
			parent.getToolPanel().add(editToolBar);
			parent.getToolPanel().add(zoomToolBar);
			parent.getToolPanel().add(modelEquilibriumToolBar);
			modelStiffness.setEnabled(true);
			modelForces.setEnabled(true);
			modelNodalForces.setEnabled(true);
			modelReduceStiffness.setEnabled(true);
			modelReduceForces.setEnabled(true);
			modelReduceNodalForces.setEnabled(true);
			modelEquilibrium.setEnabled(true);
			help5.setEnabled(true);
							
	        parent.getConsolePanel().setVisible(true);
			
			grid.setEnabled(true);
			snap.setEnabled(true);
			
			this.getDrawingArea().setRepainting(false);
			this.getDrawingArea().repaint();
	        
			parent.getConsoleHandler().setToCommandMode();
			parent.getConsoleHandler().setKeybCommand(null);
			
			this.setStateButtons();
			
			parent.getTree().setEnabled(true);
			parent.getTree().setSelectionPath(parent.getTree().getNextMatch("Model Equilibrium", 0, Position.Bias.Forward));
			
			this.getController().compose();

			parent.getDrawPanel().removeAll();
			parent.getDrawPanel().add(this.getDrawingArea(), BorderLayout.CENTER);
			this.getDrawingArea().removeAllCommands();
			this.getDrawingArea().setClickedCommand(new SelectCommand(parent));
			this.getDrawingArea().setCursor();
			parent.getDrawPanel().repaint();
			parent.getDrawPanel().updateUI();
			
			parent.getJMenuBar().updateUI();
			parent.repaint();
	    }   

	public  JFrame getInterface(){
    	return parent;
    }
    
	public void setInterface(JFrame par){
    	this.parent = (LearnInterface)par;
    }

    public void setCoord(String x, String y){
    	this.parent.setCoord(x, y);
    }
    
    public void setMessage(String m){
    	this.parent.setMessage(m);
    }

}
